<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE xsl:stylesheet SYSTEM "lang://photos">

<xsl:stylesheet
	version="1.0"
	xmlns:php="http://php.net/xsl"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
		
	<xsl:include href="../main.xsl" />
	<xsl:include href="menu.xsl" />
	<xsl:include href="../common/navigation.xsl" />
	<xsl:include href="blocks.xsl" />
	<xsl:include href="albumBar.xsl" />
	<xsl:include href="rss.xsl" />

	<xsl:template match="/data">
		<html>
		
		<head>
			<title>
				<xsl:value-of select="$pageTitle" />
			</title>
			<xsl:call-template name="meta-head" />
			<xsl:call-template name="rss">
				<xsl:with-param name="userLogin">
					<xsl:value-of select="response/@user-login" />
				</xsl:with-param>
				<xsl:with-param name="albumId">
					<xsl:value-of select="response/@album-id" />
				</xsl:with-param>
			</xsl:call-template>
			<link rel="stylesheet" href="{$baseURI}/css/photos.css" type="text/css" media="all" />
			<script type="text/javascript" src="{$baseURI}/js/photos.js" />
		</head>
		<body>
			
			<xsl:call-template name="top-userbar" />

			<div id="header">
				<xsl:call-template name="logo" />

				<xsl:call-template name="menu" />
			</div>

			<div id="blocks">				
				<div id="blocks_inner">
					<xsl:call-template name="default-blocks-top" />
					
					<xsl:if test="@method = 'album'">
						<xsl:if test="@user-group != 'guest'">
							<div class="block">
								<div id="arrow">
									<form>
										<xsl:attribute name="action">
											<xsl:if test="response/@album-owner-id = $userId">
												<xsl:value-of select="concat ($baseURI, '/photos/upload/', response/@album-id)" />
											</xsl:if>
											<xsl:if test="response/@album-owner-id != $userId">
												<xsl:text>/photos/upload</xsl:text>
											</xsl:if>
										</xsl:attribute>
										<span>
											<input type="submit" value="&labelUploadButton;" />
										</span>
									</form>
								</div>
								<p style="padding-left:30px;"><small>&labelCanCreateAlbum1; <a href="{$baseURI}/photos/create_album">&labelCanCreateAlbum2;</a></small></p>
							</div>
						</xsl:if>
						
						<div class="block">
							<h4>&labelBlockOtherAlbums;</h4>
							<div>
								<xsl:apply-templates select="document(concat('xmodule://photos/getUserAlbums/', response/@album-owner-id, ';', response/@album-id))/data/response" mode="block-user-albums" />
							</div>
						</div>
					</xsl:if>
					
					<div class="block">
						<h4>&labelBlockCloud;</h4>
						<div>
							<xsl:apply-templates select="document('xmodule://photos/getTagsCloud')/data/response/tags" mode="block-tags-cloud" />
						</div>
					</div>
				</div>
			</div>

			<div id="content">
				<div id="content_inner">
					<xsl:apply-templates select="response" />
				</div>
			</div>

			<xsl:call-template name="footer" />
		</body>
		</html>
	</xsl:template>
	
	
	
	<!-- RESPONSE -->
	<xsl:template match="response">
		<xsl:param name="albumId" select="@album-id" />
		
		<xsl:choose>
			<xsl:when test="$method = 'album'">
				<xsl:apply-templates select="document(concat('xobject://photoalbum/', $albumId))/data/object" mode="album-bar" />
			</xsl:when>
			<xsl:when test="../@method = 'tag'">
				<div id="tag_search">
					<form action="{$baseURI}/photos/tag/go" method="post">
						<input id="tag_name" type="text" name="tag">
							<xsl:attribute name="value">
								<xsl:value-of select="@tag-filter" />
							</xsl:attribute>
						</input>
						<xsl:text> </xsl:text>
						<input type="submit" value="→" />
					</form>
				</div>
				<script type="text/javascript">
					$('#tag_name')
						.blur (function () {
							if ($(this).val() == '') {
								$(this)
									.val('&labelInsertTag;')
									.css('color', '#dfdfdf');
							}
						})
						.click (function () {
							if ($(this).val() == '&labelInsertTag;') {
								$(this)
									.val('')
									.css ('color', '#000');
							}
						});
					
					
					if ($('#tag_name').val() == '') {
						$('#tag_name')
							.val('&labelInsertTag;')
							.css('color', '#dfdfdf');
					}
				</script>
			</xsl:when>
			<xsl:otherwise>
				<h1>&labelLastPhotos;</h1>
			</xsl:otherwise>
		</xsl:choose>
		
		<xsl:if test="count (object) = 0">
			<p>
				<xsl:text>&labelPhotosEmpty;.</xsl:text>
				<xsl:if test="../@method = 'album' and @album-owner-id = $userId">
					<xsl:text> </xsl:text>
					<input type="button" value="&labelUploadButton;" onclick="document.location.href='{$baseURI}/photos/upload/{$albumId}'" />
				</xsl:if>
			</p>
		</xsl:if>
		
		<xsl:if test="count (object) != 0">
			<div id="photos_list">
				<xsl:apply-templates select="object" />
			</div>
		</xsl:if>
		
		<xsl:apply-templates select="pages" mode="pages-navigation">
			<xsl:with-param name="href">
					<xsl:if test="../@method != 'album'">
						<xsl:value-of select="concat ($baseURI, '/photos')" />
					</xsl:if>
					<xsl:if test="../@method = 'album'">
						<xsl:value-of select="concat ($baseURI, '/photoalbum/', @album-id)" />
					</xsl:if>
			</xsl:with-param>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="object[@type='photo']">
		<xsl:param name="id" select="@id" />
		<xsl:param name="userLogin" select="property[@name='user_login']/value" />
		<xsl:param name="title" select="property[@name='title']/value" />
		<xsl:param name="albumId" select="property[@name='album_id']/value" />		
		<xsl:param name="countTags" select="count (property[@name='tags']/value)" />
		<xsl:param name="rating" select="number(property[@name='rating']/value)" />		
		
		<div class="photo_item">
			<div class="photo_preview">
				<div class="photo_overlay_wrapper">
					<a href="{$baseURI}/photo/{$id}#comments" title="&labelComments;" class="photo_comments">
						<xsl:value-of select="property[@name='count_comments']/value" />
					</a>
				</div>
				<a href="{$baseURI}/photo/{$id}">
					<img alt="{$title}">
						<xsl:attribute name="src">
							<xsl:value-of select="concat ($baseURI, '/thumb_100_100', property[@name='photo']/value)" />
						</xsl:attribute>
					</img>
				</a>
				<div class="photo_overlay_wrapper">
					<div class="photo_rating">
						<xsl:if test="$rating &gt; 0">
							<xsl:value-of select="concat('+', $rating)" />
						</xsl:if>
						<xsl:if test="$rating &lt; 0 or $rating = 0">
							<xsl:value-of select="$rating" />
						</xsl:if>
					</div>
				</div>
			</div>
			<div class="photo_info">
				<h3>
					<a href="{$baseURI}/photo/{$id}">
						<xsl:if test="$title = ''">
							<xsl:text>&labelWithoutName;</xsl:text>
						</xsl:if>
						<xsl:if test="$title != ''">
							<xsl:value-of select="$title" />
						</xsl:if>
					</a>
					
					<xsl:if test="property[@name='owner_id']/value = $userId or $userGroup = 'admin' or $userGroup = 'moderator'">
						<span style="margin-left:20px;">
							<a href="{$baseURI}/photos/edit/{$albumId}?photo[]={$id}">
								<img src="{$baseURI}/images/settings-icon.png" alt="" />
							</a>
							<xsl:text> </xsl:text>
							<a onclick="if(!confirm('&labelDeletePhotoConfirm;'))return false;" href="{$baseURI}/photos/delete_photo/{$id}">
								<img src="{$baseURI}/images/delete-icon.png" alt="" />
							</a>
						</span>
					</xsl:if>
				</h3>
				<div class="owner">
					<a href="{$baseURI}/user/{$userLogin}">
						<xsl:value-of select="property[@name='user_name']/value" />
					</a>
					<span>
						<xsl:value-of select="property[@name='date']/value" />
					</span>
				</div>
				<div class="album">
					<xsl:text>&labelAlbum;: </xsl:text>
					<a href="{$baseURI}/photoalbum/{$albumId}">
						<xsl:value-of select="property[@name='album_title']/value" />
					</a>
				</div>
				<xsl:if test="$countTags &gt; 0">
					<div class="tags">
						<xsl:for-each select="property[@name='tags']/value">
							<a>
								<xsl:attribute name="href">
									<xsl:value-of select="concat ($baseURI, '/photos/tag/', .)" />
								</xsl:attribute>
								<xsl:value-of select="." />
							</a>
							<xsl:if test="position() != $countTags">
								<xsl:text>, </xsl:text>
							</xsl:if>
						</xsl:for-each>
					</div>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
</xsl:stylesheet>